library(data.table)
library(ggplot2)
library(tidyverse)
library(gridExtra)
library(grid)
library(readr)
library(stringr)

paper <- TRUE


dir.project <- './'
dir.generated <- './data/generated/'
dir.fig.paper <-
  paste0(dir.project, 'doc/paper/figures/calibration/')

source('./src/r/figures.R')

fileName <- paste0(dir.project, 'calibration_version.txt')
calibration_version <-
  read_file(fileName) %>% str_replace_all("[\r\n]" , "")

dat <-
    fread(paste0(dir.generated, "r/morg_data_stacked_90.csv")) %>%
    filter(wagesample==1)

dens <-
  fread(
    paste0(
      dir.generated,
      "matlab/Calibration/",
      calibration_version,
      "_conditional_densities.csv"
    )
  )

dir.fig <- dir.fig.paper

xmin = 1
xmax = 5

lineColor = 'black'
histColor = 'gray'

densM <-
  ggplot(weight = dat$earnwt) + geom_histogram(
    data = dat %>% filter(ind1 == 1),
    aes(x = ln_w_pareto, y = ..density..),
    fill = histColor,
    binwidth = 0.1
  ) +
  theme_bw(base_size = 8) + xlim(xmin, xmax) + ylim(0, 1.5) +
  geom_line(data = dens, aes(x = ln_w, y = cond_dens_M), color = lineColor) +
  xlab('log wage') + theme.serif

ggsave(
  paste0(dir.fig, "plot_density_vs_data_FixC_M.pdf"),
  plot = densM,
  width = 5,
  height = 4,
  units = "cm"
)

densR <-
  ggplot(weight = dat$earnwt) + geom_histogram(
    data = dat %>% filter(ind2 == 1),
    aes(x = ln_w_pareto, y = ..density..),
    fill = histColor,
    binwidth = 0.1
  ) +
  theme_bw(base_size = 8) + xlim(xmin, xmax) + ylim(0, 1.5) +
  geom_line(data = dens, aes(x = ln_w, y = cond_dens_R), color = lineColor) +
  xlab('log wage') + ylab("") + theme.serif

ggsave(
  paste0(dir.fig, "plot_density_vs_data_FixC_R.pdf"),
  plot = densR,
  width = 5,
  height = 4,
  units = "cm"
)


densC <-
  ggplot(weight = dat$earnwt) + geom_histogram(
    data = dat %>% filter(ind3 == 1),
    aes(x = ln_w_pareto, y = ..density.., fill =
          histColor),
    binwidth = 0.1
  ) +
  theme_bw(base_size = 8) + theme(legend.position = "none") + xlim(xmin, xmax) + ylim(0, 1.5) +
  geom_line(data = dens, aes(x = ln_w, y = cond_dens_C), color = lineColor) +
  scale_fill_manual(
    name = "",
    guide = "legend",
    labels = c('data', 'estimated'),
    values = c(histColor, lineColor)
  ) +
  xlab('log wage') + ylab("") + theme.serif

ggsave(
  paste0(dir.fig, "plot_density_vs_data_FixC_C.pdf"),
  plot = densC,
  width = 5,
  height = 4,
  units = "cm"
)


# deciles plot
deciles <-
  fread(
    paste0(
      dir.generated,
      'matlab/Calibration/',
      calibration_version,
      '_deciles_eps.csv'
    )
  ) %>% mutate(decile = as.factor(row_number()))

# wide to long
deciles.long <- deciles %>% gather("version", "value", -decile)

ggplot(deciles.long, aes(x = decile, y = value)) + geom_col(aes(fill = version), position =
                                                              "dodge")
to.pdf(
  fig.deciles(deciles.long, fig.theme.deciles, legend = TRUE),
  "deciles_eps.pdf",
  width = 4,
  height = 2,
  git.footnote = FALSE
)

# combined in one plot
densities_combined <- grid.arrange(
  densM + ggtitle('manual'),
  densR + ggtitle('routine'),
  densC + ggtitle('cognitive'),
  ncol = 3,
  nrow = 1
)

ggsave(
  paste0(dir.fig, "plot_densities_combined.pdf"),
  plot = densities_combined,
  width = 13.5,
  height = 4,
  units = "cm"
)
